package com.duowei.diandan.fragment.dialog;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;

import com.duowei.diandan.BuildConfig;
import com.duowei.diandan.R;
import com.duowei.diandan.app.App;
import com.duowei.diandan.databinding.InputDialogFragmentBinding;

/**
 * 输入窗口
 */
public class InputDialogFragment extends BaseDialogFragment implements View.OnClickListener {

    private static final String EXTRA_TITLE = "title";
    private static final String EXTRA_TEXT = "text";
    private static final String EXTRA_IS_PASSWORD = "is_password";
    private static final String EXTRA_DOT = "dot";

    private InputDialogFragmentBinding mBinding;

    private String mTitle;
    private String mText;
    private boolean mIsPassword;
    private boolean mHasDot;

    private int mErrorAnimationNum = 3;

    public static InputDialogFragment newInstance(String title, String text, boolean hasDot) {

        Bundle args = new Bundle();
        args.putString(EXTRA_TITLE, title);
        args.putString(EXTRA_TEXT, text);
        args.putBoolean(EXTRA_DOT, hasDot);

        InputDialogFragment fragment = new InputDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static InputDialogFragment newInstanceInputPassword() {

        Bundle args = new Bundle();
        args.putString(EXTRA_TITLE, "请输入密码");
        args.putBoolean(EXTRA_IS_PASSWORD, true);
        if (BuildConfig.DEBUG) {
            args.putString(EXTRA_TEXT, App.getApp().getString(R.string.password));
        } else {
            args.putString(EXTRA_TEXT, null);
        }
        args.putBoolean(EXTRA_DOT, false);

        InputDialogFragment fragment = new InputDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mTitle = args.getString(EXTRA_TITLE);
            mText = args.getString(EXTRA_TEXT);
            mIsPassword = args.getBoolean(EXTRA_IS_PASSWORD);
            mHasDot = args.getBoolean(EXTRA_DOT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_input_dialog, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mBinding.titleView.setText(mTitle);

        mBinding.noImeEditText.setText(mText);
        if (mIsPassword) {
            mBinding.noImeEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }

        mBinding.btn0.setOnClickListener(this);
        mBinding.btn1.setOnClickListener(this);
        mBinding.btn2.setOnClickListener(this);
        mBinding.btn3.setOnClickListener(this);
        mBinding.btn4.setOnClickListener(this);
        mBinding.btn5.setOnClickListener(this);
        mBinding.btn6.setOnClickListener(this);
        mBinding.btn7.setOnClickListener(this);
        mBinding.btn8.setOnClickListener(this);
        mBinding.btn9.setOnClickListener(this);

        mBinding.btnDot.setOnClickListener(this);
        if (mHasDot) {
            mBinding.btnDot.setVisibility(View.VISIBLE);
        } else {
            mBinding.btnDot.setVisibility(View.GONE);
        }

        view.findViewById(R.id.btn_left_arrow).setOnClickListener(this);
//        view.findViewById(R.id.btn_clear).setOnStatusListener(this);
        view.findViewById(R.id.btn_ok).setOnClickListener(this);
        view.findViewById(R.id.btn_close).setOnClickListener(this);

        if (!getResources().getBoolean(R.bool.is_table)) {
            mBinding.btnOk.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mBinding.btnOk.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT, mBinding.btnOk.getHeight() + 20);
                    mBinding.buttonLayout.setLayoutParams(params);
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
                mBinding.noImeEditText.setEnabled(true);
                mBinding.noImeEditText.append(((Button) v).getText());
                break;
            case R.id.btn_dot:
                mBinding.noImeEditText.setEnabled(true);

                if (mBinding.noImeEditText.length() > 0 && !mBinding.noImeEditText.getText().toString().contains(".")) {
                    mBinding.noImeEditText.append(".");
                }
                break;
            case R.id.btn_left_arrow:
                mBinding.noImeEditText.setEnabled(true);

                String text = mBinding.noImeEditText.getText().toString();
                if (text.length() > 0) {
                    mBinding.noImeEditText.setText(text.substring(0, text.length() - 1));
                }
                break;
//            case R.id.btn_clear:
//                mInputView.setText(null);
//                break;
            case R.id.btn_close:
                if (mCancelBtnClickListener != null) {
                    mCancelBtnClickListener.onCancelBtnClick();
                }

                dismiss();
                break;
            case R.id.btn_ok:
                String input = removeLastDot(mBinding.noImeEditText.getText().toString());
                if (TextUtils.isEmpty(input)) {
                    // 没有输入处理
                    mBinding.noImeEditText.setEnabled(false);

                    AnimationSet set = (AnimationSet) AnimationUtils.loadAnimation(getContext(), R.anim.input_error);
                    mErrorAnimationNum = 0;
                    set.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (mErrorAnimationNum < 3) {
                                set.start();
                            }
                            mErrorAnimationNum++;
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
                    mBinding.noImeEditText.startAnimation(set);
                    return;
                }

                if (mOkBtnClickListener != null) {
                    mOkBtnClickListener.onOkBtnClick(input);
                }

                dismiss();
                break;
        }
    }

    private String removeLastDot(String text) {
        if (TextUtils.isEmpty(text)) {
            return text;
        }

        char last = text.charAt(text.length() - 1);
        if (last == '.') {
            text = text.substring(0, text.length() - 1);
        }
        return text;
    }

    public void setOkBtnClickListener(OnOkBtnClickListener listener) {
        mOkBtnClickListener = listener;
    }

    public void setCancelBtnClickListener(OnCancelBtnClickListener listener) {
        mCancelBtnClickListener = listener;
    }

    private OnOkBtnClickListener mOkBtnClickListener;
    private OnCancelBtnClickListener mCancelBtnClickListener;

    public interface OnOkBtnClickListener {
        /**
         * @param text 输入框中的数据
         */
        void onOkBtnClick(String text);
    }

    public interface OnCancelBtnClickListener {
        void onCancelBtnClick();
    }
}
